﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TTBusiness.DB_Linq;
using TTBusiness;
using TTBusiness.ClassDB;
using System.Data.Linq.SqlClient;
namespace TTBusiness.ClassProject
{
    public class clsBSBusiness
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();


        // Thuộc tính của sản phẩm
        public List<tblProduct_Attribute> Attribute_getByProductId(int productid)
        {
            try
            {
                var list = from pa in objDB.tblProduct_Attributes
                           where pa.ProductID.Equals(productid)
                           select pa;
                return list.ToList<tblProduct_Attribute>();
            }
            catch (Exception)
            {

                throw;
            }
        }

        // kết thúc

        public List<tblNew> NgetOtherNews(int id)
        {
            try
            {
                List<tblNew> l = new List<tblNew>();
                var list = (from t in objDB.tblNews
                            where t.ID.Equals(id)
                            select t into new1
                            from a in objDB.tblNews
                            where a.Status == true && a.CateID.Equals(new1.CateID) && a.ID < new1.ID
                            orderby a.ID descending
                            select a).Take(10);
                if (list.Count() < 10)
                {
                    var list1 = (from t in objDB.tblNews
                                 where t.ID.Equals(id)
                                 select t into new1
                                 from a in objDB.tblNews
                                 where a.Status == true && a.CateID.Equals(new1.CateID) && a.ID > new1.ID
                                 orderby a.ID descending
                                 select a).Take(10 - list.Count());
                    l.AddRange(list1);
                }
                l.AddRange(list);

                return l;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Lấy danh top tin tức
        /// </summary>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<tblNew> getTop(int top)
        {
            try
            {
                var list = (from a in objDB.tblNews
                            where a.Status == true && a.tblCateNew.TypeID.Equals(1)
                            orderby a.ID descending
                            select a).Take(top);
                return list.ToList<tblNew>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblNew> News_getByType(int type)
        {
            try
            {
                var list = from cn in objDB.tblCateNews
                           where cn.TypeID == type && cn.tblTypeNew.Status == true && cn.tblTypeNew.Visible == true
                           && cn.Status == true && cn.Visible == true
                           select cn into catenew
                           from a in objDB.tblNews
                           where a.CateID == catenew.ID && a.Status == true
                           orderby a.ID descending
                           select a;
                return list.ToList<tblNew>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblNew News_getOneByType(int type)
        {
            try
            {
                var list = (from a in objDB.tblNews
                            where a.Status == true && a.tblCateNew.TypeID.Equals(type)
                            orderby a.ID descending
                            select a).FirstOrDefault();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Lấy danh sách các sản phẩm hót
        /// </summary>
        /// <param name="hot"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<tblProduct> getTopProductHot(bool hot, int top)
        {
            try
            {
                var list = (from a in objDB.tblProducts
                            where a.Hot.Equals(hot) && a.Visible == true && a.Status == true
                            orderby a.Price ascending
                            select a).Take(top);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> getTopProductNew(bool news, int top)
        {
            try
            {
                var list = (from a in objDB.tblProducts
                            where a.News.Equals(news) && a.Visible == true && a.Status == true
                            orderby a.ID descending
                            select a).Take(top);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> getTopProductHot(bool hot)
        {
            try
            {
                var list = (from a in objDB.tblProducts
                            where a.Hot.Equals(hot) && a.Visible == true && a.Status == true
                            orderby a.Price ascending
                            select a);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> getTopProductNew(bool news)
        {
            try
            {
                var list = (from a in objDB.tblProducts
                            where a.News.Equals(news) && a.Visible == true && a.Status == true
                            orderby a.ID descending
                            select a);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> Product_getSale()
        {
            try
            {
                var list = (from a in objDB.tblProducts
                            where (a.Price > a.Sale && a.Sale > 0) && a.Visible == true && a.Status == true
                            orderby a.Sale ascending
                            select a);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> Product_getByCate(int cate)
        {
            try
            {
                var list = (from a in objDB.tblProducts
                            where a.tblCate_Factory.CateID.Equals(cate) && a.Visible == true && a.Status == true
                            orderby a.Price ascending
                            select a);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> Product_getOrther(int id, int top)
        {
            try
            {
                var list = (from a in objDB.tblProducts
                            where a.ID.Equals(id) && a.Visible == true && a.Status == true
                            select a into p1
                            from p2 in objDB.tblProducts
                            where p2.tblCate_Factory.CateID.Equals(p1.tblCate_Factory.CateID) && p2.Visible == true && p2.Status == true
                            && p2.ID > id
                            select p2).Take(top);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> Product_getByType(int type)
        {
            try
            {
                var list = (from a in objDB.tblProducts
                            where a.tblCate_Factory.tblCateProduct.TypeID.Equals(type) && a.Visible == true && a.Status == true
                            orderby a.Price ascending
                            select a);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> product_Search(string type, string name, float price)
        {
            try
            {
                List<tblProduct> l = new List<tblProduct>();
                if (type.Equals("hot"))
                {
                    if (price > 0)
                    {
                        var list = from p in objDB.tblProducts
                                   where 
                                   (p.Price.Equals(price) || (p.Sale > 0 && p.Sale.Equals(price)))
                                   && p.Hot == true && p.Status == true && p.Visible == true
                                   select p;
                        l = list.ToList<tblProduct>();
                    }
                    else
                    {
                        var list = from p in objDB.tblProducts
                                   where p.Name.ToLower().Contains(name.ToLower()) &&
                                   p.Hot == true && p.Status == true && p.Visible == true
                                   select p;
                        l = list.ToList<tblProduct>();
                    }
                }
                else
                    if (type.Equals("new"))
                    {
                        if (price > 0)
                        {
                            var list = from p in objDB.tblProducts
                                       where 
                                       (p.Price.Equals(price) || (p.Sale > 0 && p.Sale.Equals(price)))
                                       && p.News == true && p.Status == true && p.Visible == true
                                       select p;
                            l = list.ToList<tblProduct>();
                        }
                        else
                        {
                            var list = from p in objDB.tblProducts
                                       where p.Name.ToLower().Contains(name.ToLower()) &&
                                       p.News == true && p.Status == true && p.Visible == true
                                       select p;
                            l = list.ToList<tblProduct>();
                        }
                    }
                    else
                        if (type.Equals("sale"))
                        {
                            if (price > 0)
                            {
                                var list = from p in objDB.tblProducts
                                           where 
                                           (p.Price.Equals(price) || (p.Sale > 0 && p.Sale.Equals(price)))
                                           && p.Sale > 0 && p.Sale < p.Price && p.Status == true && p.Visible == true
                                           select p;
                                l = list.ToList<tblProduct>();
                            }
                            else
                            {
                                var list = from p in objDB.tblProducts
                                           where p.Name.ToLower().Equals(name.ToLower())
                                           && p.Sale > 0 && p.Sale < p.Price
                                           && p.Status == true && p.Visible == true
                                           select p;
                                l = list.ToList<tblProduct>();
                            }
                        }
                return l;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<clsBSGetOrder> Order_getOrder(List<clsBSOrder> orderlist)
        {
            try
            {
                List<clsBSGetOrder> l = new List<clsBSGetOrder>();
                foreach (var item in orderlist)
                {
                    clsBSGetOrder obj = new clsBSGetOrder();
                    clsBSProduct objB = new clsBSProduct();
                    obj.Product = objB.getById(item.ProductId);
                    obj.Count = item.Count;
                    l.Add(obj);
                }
                return l;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
